package com.auto.api.core.type.json.torna;

import cn.torna.sdk.param.DocParamResp;
import com.auto.api.common.utils.JsonMapper;
import com.auto.api.core.type.TypeFactory;
import com.auto.api.core.util.BeanUtil;
import java.util.List;
import org.springframework.core.ResolvableType;

/**
 * @program:
 * @description: TornaCommonApiResultJsonTypeHandler类型处理器
 * @author: liangshf
 * @create: 2020-09-24
 */
public class TornaCommonApiResultJsonTypeHandler extends AbstractTornaApiResultJsonTypeHandler {

    @Override
    public boolean canCovert(ResolvableType resolvableType) {
        return true;
    }

    @Override
    public String getResultJson(ResolvableType resolvableType, List<DocParamResp> responseParams) {
        if (!resolvableType.hasGenerics()
            && TypeFactory.getTypeHandler(resolvableType.resolve().getSimpleName()) != null) {
            responseParams.add(BeanUtil.getDocResponseResp(resolvableType.resolve().getSimpleName(), ""));
            return JsonMapper.toJson(responseParams);
        }

        if (resolvableType.hasGenerics()) {
            responseParams.addAll(BeanUtil.getTornaResultApi(resolvableType));
        }
        return JsonMapper.toJson(responseParams);
    }
}